class DeskController < ApplicationController


  layout "desk"
   
  before_filter :general_authorize
  before_filter :desk_authorize

  def index
    @logged_in_house = @logged_in_user.house
    @freshmen_in_house = @logged_in_house.freshmen_in_house
    @active_jaunts = @logged_in_house.active_jaunts
    @overnighting_freshmen = @logged_in_house.overnighting_freshmen
  end


  def house_enter
    entrance_time = time_from_params
    flash[:notice] = Freshman.find(params[:freshman_id]).enter_house(@logged_in_user.house_id, entrance_time, @logged_in_user.id)
    redirect_to(:action => 'update_freshmen_in_house_and_jaunts')
  end

  def house_exit
    exit_time = time_from_params
    this_freshman = Freshman.find(params[:freshman_id])
    if @logged_in_user.house_id == this_freshman.current_house_visit.house_id
      flash[:notice] = this_freshman.exit_house(exit_time, params[:destination], @logged_in_user.id)
    end
    redirect_to(:action => 'update_freshmen_in_house_and_jaunts')
  end

  def jaunt_depart
    departure_time = time_from_params
    this_freshman = Freshman.find(params[:freshman_id])
    this_jaunt = Jaunt.find(params[:jaunt_id])
    if @logged_in_user.house == Jaunt.find(params[:jaunt_id]).house
      flash[:notice] = this_freshman.jaunt_depart(params[:jaunt_id], departure_time, @logged_in_user.id)
    end
    redirect_to(:action => 'update_freshmen_in_house_and_jaunts')
  end

  def jaunt_return
    return_time = time_from_params
    this_freshman = Freshman.find(params[:freshman_id])
    if @logged_in_user.house_id == this_freshman.current_jaunt.jaunt.house_id
      flash[:notice] = this_freshman.jaunt_return(return_time, @logged_in_user.id)
    end
    redirect_to(:action => 'update_freshmen_in_house_and_jaunts')
  end

  def overnight
    wakeup_time = time_from_params
    flash[:notice] = Freshman.find(params[:freshman_id]).start_overnight(@logged_in_user.house_id, wakeup_time, @logged_in_user.id)
    redirect_to(:action => 'update_freshmen_in_house_and_jaunts')
  end

  def wakeup
    effective_at = time_from_params
    flash[:notice] = Freshman.find(params[:freshman_id]).wakeup(effective_at, @logged_in_user.id)
    redirect_to(:action => 'update_freshmen_in_house_and_jaunts')
  end
  
  def insert_jaunts_menu
    @active_jaunts = @logged_in_user.house.active_jaunts
    render(:layout=>false)
  end
  
  
  def update_freshmen_in_house_and_jaunts
    logged_in_house = @logged_in_user.house
    @freshmen_in_house = logged_in_house.freshmen_in_house
    @active_jaunts = logged_in_house.active_jaunts
    @overnighting_freshmen = logged_in_house.overnighting_freshmen
    @freshman_jaunts = @active_jaunts.map do |this_jaunt|
      this_jaunt.current_freshman_jaunts
    end
    @freshman_jaunts.flatten!
  end
  
  def insert_freshman_in_house
    @this_freshman = Freshman.find(params[:freshman_id])
    unless @this_freshman.nil? or @this_freshman.current_house_visit.house_id = @logged_in_user.house_id
      @this_freshman = nil
    end
    @insertion = true
    render(:layout=>false)
  end
  
  def insert_overnighting_freshman
    @this_freshman = Freshman.find(params[:freshman_id])
    unless @this_freshman.nil? or @this_freshman.current_overnight.house_id = @logged_in_user.house_id
      @this_freshman = nil
    end
    @insertion = true
    render(:layout=>false)
  end
  
  def insert_active_jaunt
    @this_jaunt = Jaunt.find(params[:jaunt_id])
    unless @this_jaunt.nil? or @this_jaunt.house_id = @logged_in_user.house_id
      @this_jaunt = nil
    end
    @insertion = true
    render(:layout=>false)
  end
  
  def insert_freshman_on_jaunt
    these_freshman_jaunts = Freshman.find(params[:freshman_id]).freshman_jaunts.find_all_by_jaunt_id(params[:jaunt_id])
    @this_freshman_jaunt = these_freshman_jaunts.find { |test_me|
      test_me.currently_active and test_me.jaunt.house_id = @logged_in_user.house_id
    }
    @insertion = true
    render(:layout=>false)
  end
  
  def dynamic_freshman_search
    search_name = params[:search_name]
    if search_name == ''
      @possible_freshmen = [];
    else
      all_matches = search_name.split(' ').map do |name_fragment|
        name_fragment = '%' + name_fragment + '%'
        Freshman.find(:all, :conditions => ["first_name LIKE ? OR last_name LIKE ?", name_fragment, name_fragment])
      end
      @possible_freshmen = all_matches[0]
      all_matches.each do |these_matches|
        @possible_freshmen = @possible_freshmen & these_matches
      end
      exempt_freshmen = HouseVisit.find(:all).map do |this_visit|
        if this_visit.currently_active
          this_visit.freshman
        else
          nil
        end
      end
      exempt_freshmen.compact!
      @possible_freshmen = @possible_freshmen - exempt_freshmen
    end
    render(:layout=>false)
  end
  

private

  def desk_authorize
    unless @logged_in_user.nil? or @logged_in_user.desk_priv
      if request.xhr?
        redirect_to(:controller => 'auth', :action => 'xhr_auth_failure')
      else
        flash[:notice] = "Please select a section you are authorized for!"
        redirect_to(:controller => "auth")
      end
    end
  end

end
